package com.zs.zcerp.approval.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.zcerp.approval.entity.QhBasestdMEntity;
import com.zs.zcerp.approval.entity.QhRejectRecordEntity;
import com.zs.zcerp.approval.mapper.QhRejectRecordMapper;
import com.zs.zcerp.approval.service.QhRejectRecordService;
import com.zs.zcerp.common.entity.FebsConstant;
import com.zs.zcerp.common.entity.QueryRequest;
import com.zs.zcerp.common.utils.DateUtil;
import com.zs.zcerp.common.utils.FebsUtil;
import com.zs.zcerp.common.utils.SortUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * (QhRejectRecord)表服务实现类
 *
 * @author makejava
 * @since 2021-06-16 15:01:39
 */
    @Service
    @RequiredArgsConstructor
    @DS("oracle") //从oracle库数据查询
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public class QhRejectRecordServiceImpl extends ServiceImpl<QhRejectRecordMapper, QhRejectRecordEntity> implements QhRejectRecordService {


    @Override
    public void createEntity(QhRejectRecordEntity rejectRecordEntity) {

        rejectRecordEntity.setCreateDate(new Date());
        rejectRecordEntity.setRejectId(FebsUtil.get32UUID()); //UUID
        save(rejectRecordEntity);
        

    }

    @Override
    public List<QhRejectRecordEntity> findList( QhRejectRecordEntity qhRejectRecordEntity){
        return baseMapper.findList(qhRejectRecordEntity);
    }

    @Override
    public IPage<QhRejectRecordEntity> findPageList(QhRejectRecordEntity rejectRecordEntity, QueryRequest request) {
            if (StringUtils.isNotBlank(rejectRecordEntity.getCreateTimeFrom()) &&
                    StringUtils.equals(rejectRecordEntity.getCreateTimeFrom(), rejectRecordEntity.getCreateTimeTo())) {
                rejectRecordEntity.setCreateTimeFrom(DateUtil.formatDateTime(rejectRecordEntity.getCreateTimeFrom()));
                rejectRecordEntity.setCreateTimeTo(DateUtil.formatDateTime(rejectRecordEntity.getCreateTimeTo()));
            }
            Page<QhRejectRecordEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
            page.setSearchCount(false);
            page.setTotal(baseMapper.countDetail(rejectRecordEntity));
            SortUtil.handlePageSort(request, page, "createDate", FebsConstant.ORDER_DESC, false);
            return baseMapper.findPageList(page, rejectRecordEntity);
        }

    @Override
    public void deleteBaseByRejectId(String[] rejectIds) {
        List<String> list = Arrays.asList(rejectIds);
        baseMapper.delete(new QueryWrapper<QhRejectRecordEntity>().lambda().in(QhRejectRecordEntity::getOrderNo, list));
    }


}